package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.application.command.MessageSendCommand;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.Person;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.MessageRemoteService;
import com.smedi.ismedi.notification.api.dto.request.SendSmsRequest;
import com.smedi.ismedi.notification.api.feign.RemoteSmsNotificationFeignApi;
import com.smedi.ismedi.web.starter.config.AsyncThreadPoolConfig;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther Cookie
 * @date 2022/7/25
 */
@Service
@AllArgsConstructor
public class MessageRemoteServiceImpl implements MessageRemoteService {

    private final RemoteSmsNotificationFeignApi remoteSmsNotificationFeignApi;

    private final AdapterConverter adapterConverter;

    @Override
    public void sendMessage(Person person, String templateName) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        List<String> receiversNameList = new ArrayList<>();
        List<String> receiversMobileList = new ArrayList<>();
        receiversNameList.add(person.getName());
        receiversMobileList.add(person.getMobile());
        sendSmsRequest.setReceivers(receiversMobileList);
        sendSmsRequest.setSmsTemplateName(templateName);    //通知公告审批对应TemplateId
        sendSmsRequest.setParameters(receiversNameList);
        remoteSmsNotificationFeignApi.postSendShortMessage(sendSmsRequest);
    }

    @Override
    public void sendMessage(MessageSendCommand messageSendCommand) {
        SendSmsRequest sendSmsRequest = adapterConverter.sendMessageCommandToSendSmsRequest(messageSendCommand);
        remoteSmsNotificationFeignApi.postSendShortMessage(sendSmsRequest);
    }

    @Override
    @Async(AsyncThreadPoolConfig.asyncThreadName)
    public void sendMessage(List<MessageSendCommand> messageSendCommands) {
        List<SendSmsRequest> sendSmsRequests = adapterConverter.sendMessageCommandToSendSmsRequest(messageSendCommands);
        remoteSmsNotificationFeignApi.postSendShortMessage(sendSmsRequests);
    }
}
